package com.bnyy.medicalHousekeeper.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.result.ActivityResultLauncher;

import com.blankj.utilcode.util.SizeUtils;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.base.BaseActivityImpl;
import com.bnyy.medicalHousekeeper.request.BaseObserverImpl;
import com.bnyy.medicalHousekeeper.request.RequestManager;

import java.util.HashMap;

import butterknife.BindView;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2023/3/13
 **/
public class EditIntroductionActivity extends BaseActivityImpl {
    @BindView(R.id.et)
    EditText et;

    public static void show(Context context,String introduction, ActivityResultLauncher<Intent> launcher) {
        Intent intent = new Intent(context, EditIntroductionActivity.class);
        intent.putExtra("introduction", introduction);
        launcher.launch(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_edit_introduction;
    }

    @Override
    public String getTitleStr() {
        return "编辑简介";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TextView tv = new TextView(mContext);
        tv.setTextAppearance(R.style.NormalTextView);
        tv.setText("保存");
        tv.setTextColor(Color.WHITE);
        int dp5 = SizeUtils.dp2px(5);
        tv.setPadding(dp5, 0, dp5, 0);
        tv.setBackgroundResource(R.drawable.selector_button_corner_green_180);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String introduction = et.getText().toString();
                HashMap<String, String> params = new HashMap<>();
                params.put("introduce", introduction);
                requestManager.request(requestManager.mJavaRetrofitService.editIntroduction(RequestManager.getJsonRequestBody(params)),
                        new BaseObserverImpl<Object>(mContext) {
                            @Override
                            public void onSuccess() {
                                super.onSuccess();
                                Toast.makeText(mContext, "保存成功", Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent();
                                intent.putExtra("introduction", introduction);
                                setResult(RESULT_OK, intent);
                                EditIntroductionActivity.this.finish();
                            }
                        });
            }
        });
        setOptions(tv);

        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                tv.setEnabled(!TextUtils.isEmpty(s));
            }
        });

        String introduction = getIntent().getStringExtra("introduction");
        if (introduction == null) {
            introduction = "";
        }
        et.setText(introduction);
    }
}



















































